<template>
    <div>
        <!------用户管理查询按钮------>
        <div class="search">
            <el-input v-model="name" placeholder="请输入会议名称" suffix-icon="el-icon-search" style="width: 220px;"
                      @keyup.enter.native="loadPost">
            </el-input>

            <el-button type="primary" style="margin-left: 10px;" @click="loadPost">查询</el-button>
            <el-button type="warning" @click="resetParam">重置</el-button>
            <el-button type="success" style="margin-left: 10px;" @click="add" v-if="users.userIdentity===1">新增
            </el-button>
        </div>

        <!------信息展示列表------>
        <el-table :data="tableData"
                  :header-cell-style="{color:'#c71111'}">
<!--            <el-table-column prop="id" label="ID" width="50">
            </el-table-column>-->
            <el-table-column type="index" label="序号" width="50">
            </el-table-column>

            <el-table-column prop="name" label="会议名称" width="200">
            </el-table-column>

            <el-table-column prop="type" label="会议类型" width="150" sortable>
                <template slot-scope="scope">
                    <el-tag
                            :type="scope.row.type == 1 ? 'danger' : scope.row.type == 2 ? 'warning' :'success'"
                            disable-transitions>{{scope.row.type == 1 ? '支部大会' : (scope.row.type==2?'支部委员会':'党小组会议')}}
                    </el-tag>
                </template>
            </el-table-column>

            <el-table-column prop="location" label="会议地点" width="160">
            </el-table-column>

            <el-table-column prop="time" label="会议时间" width="220" sortable>
            </el-table-column>

            <el-table-column prop="organization" label="组织" :formatter="formatOrganizations">
            </el-table-column>

            <el-table-column prop="operate" label="操作">
                <template slot-scope="scope">
                    <el-button size="small" type="success" @click="mod(scope.row)"
                               v-if="users.userIdentity===1">编辑
                    </el-button>
                    <el-popconfirm
                            title="确定删除吗？"
                            @confirm="del(scope.row.id)"
                            style="margin-left: 10px;">
                        <el-button slot="reference" size="small" type="danger"
                                   v-if="users.userIdentity===1">删除
                        </el-button>
                    </el-popconfirm>
                    <el-button type="primary" v-if="scope.row.fileUrl" size="small" style="margin-left: 10px;"
                               @click="window.open(scope.row.fileUrl)" :disabled="scope.row.downFile">
                        下载文件
                    </el-button>
                </template>
            </el-table-column>
        </el-table>


        <!------分页查询------>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[ 5, 9, 18, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>


        <!------新增列表------>
        <el-dialog
                title="组织生活管理"
                :visible.sync="centerDialogVisible"
                width="30%"
                center>

            <el-form ref="form" :rules="rules" :model="form" label-width="80px">

                <el-form-item label="会议名称" prop="name">
                    <el-col :span="20">
                        <el-input v-model="form.name"></el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="会议类型" prop="type">
                    <el-radio-group v-model="form.type">
                        <el-radio label="1">支部大会</el-radio>
                        <el-radio label="2">支部委员会</el-radio>
                        <el-radio label="3">党小组会议</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="会议地点" prop="location">
                    <el-col :span="20">
                        <el-input v-model="form.location"></el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="会议时间" prop="time">
                    <el-col :span="20">
                        <el-date-picker
                                v-model="form.time"
                                type="datetime"
                                placeholder="选择日期时间">
                        </el-date-picker>
                    </el-col>
                </el-form-item>

                <el-form-item label="会议文件" prop="file">
                    <el-col :span="20">
                        <el-upload
                                class="upload-demo"
                                :action="'http://'+ serverIp +':8090/sys-file/upload'"
                                :on-success="handleSuccess"
                                :before-upload="handleBeforeUpload"
                                multiple
                                :limit="1"
                                :file-list="fileList">
                            <el-button size="small" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip">
                                文件最大10M
                            </div>
                        </el-upload>
                    </el-col>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {serverIp} from "../../../public/config";

    export default {
        name: "MebManage",
        data() {
            return {
                serverIp:serverIp,
                window: window,
                users: JSON.parse(sessionStorage.getItem('CurUser')),
                fileList: [],
                organizationsData: [],
                tableData: [],
                pageSize: 9,
                pageNum: 1,
                total: 0,
                name: '',

                centerDialogVisible: false,
                form: {
                    id: '',
                    name: '',
                    type: '',
                    location: '',
                    time: '',
                    organization: '',
                    fileUrl: '',
                },
                rules: {
                    name: [
                        {required: true, message: '请输入名字', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '请选择类型', trigger: 'blur'}
                    ],
                    location: [
                        {required: true, message: '请输入地点', trigger: 'blur'}
                    ],
                    time: [
                        {required: true, message: '请选择时间', trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            handleSuccess(res) {
                if (res) {
                    console.log(res)
                    this.form.fileUrl = res;
                    this.$message.success("上传成功")
                } else {
                    this.$message.error("上传失败");
                }
            },
            handleBeforeUpload(file) {
                const isLt10M = file.size / 1024 / 1024 < 10;
                if (!isLt10M) {
                    this.$message.error('上传文件大小不能超过 10MB!');
                }
                return isLt10M;
            },

            formatOrganizations(row) {
                let temp = this.organizationsData.find(item => {
                    return item.code == row.organization
                });

                return temp && temp.name
            },

            mounted() {
                // 获取当前用户信息
                const users = JSON.parse(sessionStorage.getItem('CurUser'))
                // 将当前用户的机构名赋值给 form 对象的 organization 属性
                this.form.organization = users.organization
            },

            resetForm() {
                this.$refs.form.resetFields();
            },

            del(id) {
                console.log(id);

                this.$axios.get('http://'+ serverIp +':8090/meetings/delete?id=' + id)
                    .then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                        this.centerDialogVisible = false;
                        this.loadPost();
                    } else {
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    }
                })
            },

            mod(row) {
                console.log(row);

                this.centerDialogVisible = true
                this.$nextTick(() => {
                    //赋值到表单
                    this.form.id = row.id;
                    this.form.name = row.name;
                    this.form.type = row.type + '';
                    this.form.time = row.time;
                    this.form.location = row.location;
                    this.form.organization = row.organization;
                })
            },

            doMod() {
                this.$axios.post('http://'+ serverIp +':8090/meetings/update', this.form)
                    .then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                        this.centerDialogVisible = false;
                        this.loadPost();
                        this.resetForm()
                    } else {
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    }
                })
            },

            doSave() {
                this.$axios.post('http://'+ serverIp +':8090/meetings/save', this.form)
                    .then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.$message({
                            message: '操作成功！',
                            type: 'success'
                        });
                        this.centerDialogVisible = false;
                        this.loadPost();
                        this.resetForm()
                    } else {
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    }
                })
            },

            save() {
                this.$refs.form.validate((valid) => {
                        if (valid) {
                            if (this.form.id) {
                                this.doMod();
                            } else {
                                this.doSave();
                            }
                        } else {
                            console.log('error!!!');
                            return false;
                        }
                    }
                )
            },

            add() {
                this.centerDialogVisible = true;
                this.$nextTick(() => {
                    this.resetForm();
                    this.mounted()
                })
            },

            resetParam() {
                this.name = '';
                this.userIdentity = '';
                this.loadPost()
            },

            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageNum = 1;
                this.pageSize = val;
                this.loadPost()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.pageNum = val;
                this.loadPost()
            },

            loadGet() {
                this.$axios.get('http://'+ serverIp +':8090/meetings/list').then(res => res.data).then(res => {
                    console.log(res)
                })
            },

            loadPost() {
                const users = JSON.parse(sessionStorage.getItem('CurUser'));
                this.$axios.post('http://'+ serverIp +':8090/meetings/page', {
                    pageSize: this.pageSize,
                    pageNum: this.pageNum,

                    param: {
                        name: this.name,
                        organization: users.organization,
                    }
                }).then(res => res.data).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        this.tableData = res.data
                        this.total = res.total
                    } else {
                        alert('获取数据失败')
                    }

                })
            },
            loadOrganizations() {
                this.$axios.get('http://'+ serverIp +':8090/organizations/list').then(res => res.data).then(res => {
                    console.log(res)
                    if (res.code == 200) {
                        this.organizationsData = res.data
                    } else {
                        alert('获取数据失败')
                    }

                })
            }
        },

        beforeMount() {
            // this.loadGet();
            this.loadPost();
            this.loadOrganizations();

        }
    }
</script>

<style scoped>

    .search {
        margin-bottom: 5px;
    }
</style>